<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pub extends CI_Controller {

	var $_login_userid;
	
	function __construct()
	 {
	  
		parent::__construct();
		//$this->_login_userid=0;
		$this->_login_userid=$this->get_login_userid();
	 }
    
	public function index($id){
		$this->load->database();
		$this->db->where('taskid', $id); 	
		$this->db->order_by('in_date','desc');
		$this->db->limit(1,0);
		$query = $this->db->get('article');
		if ($query->num_rows() > 0)
		{
			$row = $query->row_array(); 
			$data=$row;		
		}else{
			$pagedata=$this->get_share_pagedata();
			$pagedata['main_content']="这个节点没被发布！";
			$this->load->view('htmlpage/page_for_mess',$pagedata);
			return;
		}
		$this->load->view('pub/pub',$data);
	}
	
	public function list(){
		$this->load->database();
		// $this->db->where('taskid', $id); 	
		$this->db->order_by('in_date','desc');
		// $this->db->limit(1,0);
		$query = $this->db->get('pub');
		if ($query->num_rows() > 0)
		{
			$rows = $query->result_array(); 
			$data['list']=$rows;		
		}else{
			$pagedata=$this->get_share_pagedata();
			$pagedata['main_content']="这个节点没被发布！";
			$this->load->view('htmlpage/page_for_mess',$pagedata);
			return;
		}
		$this->load->view('pub/publist',$data);
	}

    public function node($id){
		$this->load->database();
		$this->db->where('taskid', $id); 	
		$this->db->order_by('in_date','desc');
		$this->db->limit(1,0);
		$query = $this->db->get('pub');
		if ($query->num_rows() > 0)
		{
			$row = $query->row_array(); 
			$data=$row;		
		}else{
			$pagedata=$this->get_share_pagedata();
			$pagedata['main_content']="这个节点没被发布！";
			$this->load->view('htmlpage/page_for_mess',$pagedata);
			return;
		}
		$this->load->view('pub/pubnode',$data);
	}
	
	public function doc($id){
		$this->load->database();
		$this->db->where('taskid', $id); 	
		$this->db->order_by('in_date','desc');
		$this->db->limit(1,0);
		$query = $this->db->get('pub');
		if ($query->num_rows() > 0)
		{
			$row = $query->row_array(); 
			$data=$row;		
		}else{
			$pagedata=$this->get_share_pagedata();
			$pagedata['main_content']="这个节点没被发布！";
			$this->load->view('htmlpage/page_for_mess',$pagedata);
			return;
		}
		$this->load->view('pub/pubtext',$data);
	}
	
    private function get_share_pagedata(){
		$pagedata['htmlhead']=$this->load->view('htmlpage/htmlhead','',true);
		$pagedata['logobar']=$this->load->view('htmlpage/logobar','',true);
		$pagedata['navbar']=$this->load->view('htmlpage/navbar','',true);
		$pagedata['footbar']=$this->load->view('htmlpage/footbar','',true);
		return $pagedata;
	}
	private function get_login_userid(){
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		if ($this->tank_auth->is_logged_in()){
			return $this->tank_auth->get_user_id();
		}else{
			return 0;
		}
	}
	private function get_tree_node($id){
		$this->load->database();	
		$this->db->where('id', $id); 
		$this->db->where('pub', 1);
		$query1 = $this->db->get('task');
		if ($query1->num_rows() > 0){
		    $row = $query1->row_array(); 
			$row['children']==$this->getchild($id);
		    $query1->free_result(); 
//			print_r($row);
		}else{
			$row['title']="这个节点没被发布！";
			$row['id']=0;
			$row['children']==null;
		}
		return $row;
	}

	private function getchild2($parent){
		$this->load->database();
		$this->db->where('parent', $parent); 	
//		$this->db->where('uid', $this->_login_userid);
		$this->db->order_by('sort','desc');
		$query = $this->db->get('task');
		if ($query->num_rows() > 0)
		{
			$rows_arr=array();
			foreach ($query->result_array() as $row)
			{
//				echo $row['title'];
//				echo $row['comment'];
				$rows_arr[$row['id']]=$row;
				
			}
//			print_r($rows_arr);
//			$json_string = json_encode($rows_arr);    
//			echo $json_string;  
		}
		return $rows_arr;
	}

	

	
	private function task_to_array($id){
		$this->db->where('id', $id); 
		$this->db->where('pub', 1);
		$query1 = $this->db->get('task');
		if ($query1->num_rows() > 0){
		    $row = $query1->row_array(); 
		    $query1->free_result(); 
//			print_r($row);
		}else{
			$row['title']="这个节点没被发布！";
			$row['id']=0;
			return $row;
		}
		
		

		$this->db->where('parent', $id); 	
//		$this->db->where('uid', $this->_login_userid);
		$this->db->order_by('sort','desc');
		$query = $this->db->get('task');
		if ($query->num_rows() > 0)
		{
			$row['children']=$query->result_array();
			foreach ($query->result_array() as $row)
			{
//				echo $row['title'];
//				echo $row['comment'];
				$row['children']=getchild($row['id']);
				
			}
		}	
			$rows_arr=array();
			
//			print_r($rows_arr);
//			$json_string = json_encode($rows_arr);    
//			echo $json_string;  
		return $rows_arr;
	}

	

	public function insertdom(){

			$this->load->database();
			$this->db->where('taskid', $_REQUEST['taskid']); 	
			$this->db->where('uid', $this->_login_userid);
		$query = $this->db->get('pub');
		if ($query->num_rows() > 0)
		{
			$sql = "UPDATE   pub set title="
			.$this->db->escape($_REQUEST['title'])
			.",content="
			.$this->db->escape($_REQUEST['content'])
			.",in_date='"
			.date("Y-m-d H:i:s")
			."' WHERE taskid="
			.$this->db->escape($_REQUEST['taskid'])
			." and uid="
			. $this->_login_userid;
			
				}else{
			$sql = "INSERT INTO pub (title,content, taskid,in_date,uid) 
			VALUES (".$this->db->escape($_REQUEST['title']).",".$this->db->escape($_REQUEST['content']).", ".$this->db->escape($_REQUEST['taskid']).",'".date("Y-m-d H:i:s")."','". $this->_login_userid."')";
		}	
			$query->free_result(); 
			$this->db->query($sql);
			if($this->db->affected_rows()>0){
				$status=1;
			}else{
				$status=0;
			}
			$result_arr = array(
				   'status'=> $status,
				   'message'=>'消息'.$this->db->last_query(),
				   'data' => array(
						'newid'=>$this->db->insert_id()
				   )
				);
				
			if($this->_login_userid==0){
				$result_arr['status'] = 0;
				$result_arr['message'] = '你未登录或已经过期了，不能发布内容，请先登录！';
			}		
			
		$json_string = json_encode($result_arr);    
		echo $json_string; 	
	}
	
		public function insert(){

			$this->load->database();
			$this->db->where('taskid', $_REQUEST['taskid']); 	
			$this->db->where('uid', $this->_login_userid);
		$query = $this->db->get('article');
		if ($query->num_rows() > 0)
		{
			$sql = "UPDATE   article set title="
			.$this->db->escape($_REQUEST['title'])
			.",content="
			.$this->db->escape($_REQUEST['content'])
			.",in_date='"
			.date("Y-m-d H:i:s")
			."' WHERE taskid="
			.$this->db->escape($_REQUEST['taskid'])
			." and uid="
			. $this->_login_userid;
			
				}else{
			$sql = "INSERT INTO article (title,content, taskid,in_date,uid) 
			VALUES (".$this->db->escape($_REQUEST['title']).",".$this->db->escape($_REQUEST['content']).", ".$this->db->escape($_REQUEST['taskid']).",'".date("Y-m-d H:i:s")."','". $this->_login_userid."')";
		}	
			$query->free_result(); 
			$this->db->query($sql);
			if($this->db->affected_rows()>0){
				$status=1;
			}else{
				$status=0;
			}
			$result_arr = array(
				   'status'=> $status,
				   'message'=>'消息'.$this->db->last_query(),
				   'data' => array(
						'newid'=>$this->db->insert_id()
				   )
				);
				
			if($this->_login_userid==0){
				$result_arr['status'] = 0;
				$result_arr['message'] = '你未登录或已经过期了，不能发布内容，请先登录！';
			}		
			
		$json_string = json_encode($result_arr);    
		echo $json_string; 	
	}

	
}
